# coding:utf-8
from django.shortcuts import render_to_response
from django.http import HttpResponse
import json
import sqlite3


# Create your views here.
def static_view(request, pagename):
    return render_to_response(pagename + '.html')


def index(request):
    return render_to_response('index.html')


def person(request):
    result = {'name': 'Rose', 'age': 27, 'sex': 'female'}
    return HttpResponse(json.dumps(result), content_type='application/json')


def cardBIN(request):
    cx = sqlite3.connect('tools.db')
    cu = cx.cursor()
    cu.execute('SELECT bank_name, bank_code, card_BIN, card_length, card_type FROM cardBIN')
    data = cu.fetchall()
    return HttpResponse(json.dumps(data), content_type='application/json')


def testAddress(request):
    cx = sqlite3.connect('tools.db')
    cu = cx.cursor()
    cu.execute('SELECT name, address, system, remark FROM testAddress')
    data = cu.fetchall()
    return HttpResponse(json.dumps(data), content_type='application/json')


def add_testAddress(request):
    if request.method == 'POST':
        cx = sqlite3.connect('tools.db')
        cu = cx.cursor()
        req = json.loads(request.body)
        cu.execute("INSERT INTO testAddress VALUES (:name, :address, :system, :remark)", req)
        cx.commit()
    return HttpResponse('保存成功!')
